package com.rosense.client.i;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.rosense.client.rpc.RPCBean;
import com.rosense.client.rpc.RPCCpuBean;
import com.rosense.client.rpc.RPCMemBean;
import com.rosense.client.rpc.RPCNetBean;
import com.rosense.commons.DateUtils;
import com.rosense.commons.LangUtils;
import com.rosense.commons.NumberUtils;
import com.rosense.commons.StringsUtils;

/**
 * @author 李岩飞
 * @email eliyanfei@126.com	
 * 2018年1月24日 下午2:22:21
 * 
 */
public class WRNet extends IWriteReader {

	@Override
	public String getName() {
		return "net";
	}

	@Override
	public String getText() {
		return "网络";
	}

	@Override
	public void write(String host, RPCBean bean) {
		RPCNetBean netBean = (RPCNetBean) bean;
		StringBuilder sb = new StringBuilder();
		sb.append(System.currentTimeMillis()).append(",");
		sb.append(netBean.rx).append(",");
		sb.append(netBean.tx);
		append(host, sb.toString());
	}

	@Override
	public List<? extends RPCBean> read(String host) {
		List<RPCNetBean> list = new ArrayList<RPCNetBean>();
		String value = readLastLine(host);
		String[] values = value.trim().split("\n");
		for (String line : values) {
			String[] cols = line.split(",");
			RPCNetBean net = new RPCNetBean();
			net.time = LangUtils.toLong((cols[0]));
			net.rx = LangUtils.toFloat(cols[1], 0.0f);
			net.tx = LangUtils.toFloat(cols[2], 0.0f);
			list.add(net);
		}
		return list;
	}

	@Override
	protected void parserLine(String line, Map<String, String> map) {
		String[] cols = line.split(",");
		RPCNetBean net = new RPCNetBean();
		net.time = LangUtils.toLong((cols[0]));
		net.rx = LangUtils.toFloat(cols[1], 0.0f);
		net.tx = LangUtils.toFloat(cols[2], 0.0f);
		map.put("time", DateUtils.formatDate(new Date(net.time), "yyyyMMddHHmm"));
		map.put("value", "输入:" + NumberUtils.formatNumber("0.00", net.rx) + "M,输出:" + NumberUtils.formatNumber("0.00", net.tx) + "M");
	}
	@Override
	protected void getAvgInfo(Map<String, List<RPCBean>> mapInfo,Map<String,String> map,double limit){
		float total1 = 0;
		float total2 = 0;
		float totalNum = 0;
		String str1 = "";
		String str2 = "";
		String data1 = "";
		String data2 = "";
		String xdata = "";
		int n = 0;
		for(String time : mapInfo.keySet()){
			List<RPCBean> list = mapInfo.get(time);
			totalNum += list.size();
			float value1 = 0;
			float value2 = 0;
			for(int i=0;i<list.size();i++){
				RPCNetBean net = (RPCNetBean) list.get(i);
				value1 += LangUtils.toFloat(NumberUtils.formatNumber("0.00", net.rx),0);
				value2 += LangUtils.toFloat(NumberUtils.formatNumber("0.00", net.tx),0);
			}
			total1 += value1;
			total2 += value2;
			value1 = (float)NumberUtils.formatFmZero(value1,list.size());
			value2 = (float)NumberUtils.formatFmZero(value2,list.size());
			data1 +=value1;
			data2 +=value2;
			xdata +=time;
			if(value1>limit){
				if(StringsUtils.isBlank(str1)){
					str1 += "输入:";
				}else{
					str1 += ",";
				}
				str1 += time;
			}
			if(value2>limit){
				if(StringsUtils.isBlank(str2)){
					str2 += "<br/>输出:";
				}else{
					str2 += ",";
				}
				str2 += time;
			}
			if(n++<mapInfo.size()-1){
				data1 += ",";
				data2 += ",";
				xdata += ",";
			}
		}
		if(StringsUtils.isNotBlank(str1)||StringsUtils.isNotBlank(str1)){
			map.put("hour", str1+str2);
			map.put("day", "输入："+ NumberUtils.formatNumber("0.00", total1,totalNum)+"<br/>输出:"+NumberUtils.formatNumber("0.00", total2,totalNum));
		}
		//历史的折线图
		if(limit<0){
			map.put("name1", "输入流量(M)");
			map.put("name2", "输出流量(M)");
			map.put("xdata", xdata);
			map.put("data1", data1);
			map.put("data2", data2);
		}
	}
	@Override
	protected void parserLineToMap(String line,Map<String,List<RPCBean>> map) {
		String[] cols = line.split(",");
		RPCNetBean net = new RPCNetBean();
		net.time = LangUtils.toLong((cols[0]));
		net.rx = LangUtils.toFloat(cols[1], 0.0f);
		net.tx = LangUtils.toFloat(cols[2], 0.0f);
		String time = DateUtils.formatDate(new Date(net.time), "HH");
		List<RPCBean> list = map.get(time);
		if(list==null){
			list = new ArrayList<RPCBean>();
			map.put(time, list);
		}
		list.add(net);
	}
}
